package com.zb.activemq.have_two_try;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.ScheduledMessage;

import javax.jms.*;

/**
 * Created by dongyuncong on 2017/8/9.
 */
public class ObjectProducer {

    static ConnectionFactory connectionFactory;


    static {
        connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnectionFactory.DEFAULT_USER,
                ActiveMQConnectionFactory.DEFAULT_PASSWORD,
                "tcp://39.108.78.245:61616");


    }

    /**
     * 延时的支持需要在 activeMQ 的 activemq.xml配置的
     *  <broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost" schedulerSupport="true" dataDirectory="${activemq.base}/data"
     />中加上schedulerSupport="true"
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception{

//        ProducerThread thread = new ProducerThread();
//        new Thread(thread).start();

      /*  for (int k = 0; k < 200; k++) {
            for (int i = 0; i < 50; i++) {
                ProducerThread thread = new ProducerThread();
                new Thread(thread).start();
            }
            Thread.sleep(300);
        }
*/
      while (true){
          for (int i = 0; i < 50; i++) {
              ProducerThread thread = new ProducerThread();
              new Thread(thread).start();
          }
          Thread.sleep(300);
      }
    }

    static class ProducerThread implements Runnable {


        public void run() {
            Connection connection = null;
            try {
                connection = connectionFactory.createConnection();
                connection.start();
                Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
                Destination destination = session.createQueue("ObjectQueue");
                MessageProducer producer = session.createProducer(destination);
                producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
                producer.setPriority(1);
                producer.setDisableMessageTimestamp(true);
//                producer.setTimeToLive(10000L);
                sendMessage(session, producer);
                session.commit();
            } catch (Exception e) {
                System.out.println(e);
            } finally {
                if (null != connection)
                    try {
                        connection.close();
                    } catch (Exception e) {
                        System.out.println(e);
                    }
            }

        }

        public void sendMessage(Session session, MessageProducer producer) throws Exception {
            Bean bean = new Bean("王小二", "18568956253", 21, "jaguar");
            ObjectMessage message = session.createObjectMessage(bean);
            long time = 60 * 10000;// 延时1min
            long period = 10 * 1000;// 每个10s
            int repeat = 6;// 6次
            message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, time);
            message.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_PERIOD, period);
            message.setIntProperty(ScheduledMessage.AMQ_SCHEDULED_REPEAT, repeat);
//            message.setStringProperty(ScheduledMessage.AMQ_SCHEDULED_CRON, "0/5 * * * * ?");
            producer.send(message);
        }
    }


}
